package cn.dgiot.fd.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.dgiot.fd.dao.IDevicePLCDAO;
import cn.dgiot.fd.model.DevicePLC;
import cn.dgiot.fd.service.IDevicePLCService;
import cn.dgiot.fd.service.IDeviceStateService;

/**
 * [下位机型号记录]
 * @author
 *
 */
@Service
public class DevicePLCServiceImpl implements IDevicePLCService{

	@Autowired
	private IDevicePLCDAO devicePLCDAO;

	@Autowired
	private IDeviceStateService deviceStateService;
	
	@Override
	public DevicePLC getDevicePLC(DevicePLC devicePLC) {
		return devicePLCDAO.getDevicePLC(devicePLC);
	}

	@Override
	public void insertDevicePLC(DevicePLC devicePLC) {
		// 产品key
		String productKey = devicePLC.getProductKey();
		// 设备名称
		String deviceName = devicePLC.getDeviceName();
		// 下位机标识
		String dpno = devicePLC.getDpno();
		
		List<DevicePLC> deviceDevicePLCList = new ArrayList<DevicePLC>(); 
		DevicePLC result = devicePLCDAO.getDevicePLC(new DevicePLC(deviceName, productKey, dpno));
		
		DevicePLC param = new DevicePLC();
		param.setDeviceCode(deviceName);
		param.setProductKey(productKey);
		param.setPlcCode(devicePLC.getPlcCode());
		param.setDpno(devicePLC.getDpno());
		param.setUpdateBy("system");
		param.setUpdateTime(new Date());
		if(result != null) {
			devicePLCDAO.updateDevicePLC(param);
		}else {
	    	param.setCreateBy("system");
	    	param.setCreateTime(new Date());
			deviceDevicePLCList.add(param);
		}
		
		if(deviceDevicePLCList != null && deviceDevicePLCList.size() > 0) {
			devicePLCDAO.insertDevicePLC(deviceDevicePLCList);
		}
	}

	@Override
	public void updateDevicePLC(DevicePLC devicePLC) {
		// TODO Auto-generated method stub
		devicePLCDAO.updateDevicePLC(devicePLC);
	}

}
